﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace RenameEpisodes
{
    public class EpisodeId
    {
        public int Season, Chapter;
        public EpisodeId()
        {
            Season = -1; Chapter = -1;
        }
        public EpisodeId(int season, int chapter)
        {
            Season = season;
            Chapter = chapter;
        }
        public override bool Equals(object obj)
        {
            if (obj is EpisodeId)
            {
                EpisodeId other = (EpisodeId)obj;
                return Season == other.Season && Chapter == other.Chapter;
            }
            else
                return false;
        }
        public bool ParseString(string cadena)
        {
            // TIPO 1x14
            Match match = format1.Match(cadena);
            if (match.Success)
            {
                int separatorIndex;
                string chapterId = match.Value.ToLower();
                if ((separatorIndex = chapterId.IndexOf("x")) > -1)
                {
                    Season = int.Parse(chapterId.Substring(0, separatorIndex));
                    Chapter = int.Parse(chapterId.Substring(separatorIndex + 1));
                    return true;
                }
                else
                {
                    return false;
                }
            }
            // Tipo S03E08
            match = format2.Match(cadena);
            if (match.Success)
            {
                int separatorIndex;
                string chapterId = match.Value.ToLower();
                if ((separatorIndex = chapterId.IndexOf("e")) > -1)
                {
                    Season = int.Parse(chapterId.Substring(1, separatorIndex - 1));
                    Chapter = int.Parse(chapterId.Substring(separatorIndex + 1));
                    return true;
                }
                else
                {
                    return false;
                }
            }

            // Tipo 212
            match = format3.Match(cadena);
            if (match.Success)
            {

                string chapterId = match.Value.ToLower();
                Season = int.Parse(chapterId.Substring(0, 1));
                Chapter = int.Parse(chapterId.Substring(1));
                return true;

            }
            // Tipo no reconocido
            return false;
        }
        public override int GetHashCode()
        {
            return base.GetHashCode();
        }
        private static Regex format1 = new Regex(@"\dx\d\d", RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.Singleline);
        private static Regex format2 = new Regex(@"S\d\dE\d\d", RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.Singleline);
        private static Regex format3 = new Regex(@"\d\d\d", RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.Singleline);
    }
}
